package com.neusoft.neuiotms.auth.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.neusoft.bizcore.auth.converter.AuthUserConverter;
import com.neusoft.bizcore.auth.dto.AuthUserDTO;
import com.neusoft.bizcore.auth.model.AuthOganization;
import com.neusoft.bizcore.auth.model.AuthUser;
import com.neusoft.bizcore.auth.repository.AuthOganizationRepository;
import com.neusoft.bizcore.auth.service.AuthUserService;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.common.constant.AHConstants;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api("用户管理")
@RestController
@RequestMapping(value = "/api/w/auth/users")
public class AuthUserExController {

    @Autowired
    private AuthUserService authUserService;
    @Autowired
    private AuthOganizationRepository authOganizationRepository;
    @Autowired
    private AuthUserConverter authUserConverter;

    @ApiOperation("分页查询所有用户")
    @GetMapping("/ex/p")
    public PageResultDTO<AuthUserDTO> search(final Pageable pageable, final Searchable searchable) {
        if (searchable.hasKey("belongs")) {
            final String belongs = searchable.getStrValue("belongs");
            if (AHConstants.BELONG_KJ_CODE.equals(belongs)) {
                final List<String> slRoles = Lists.newArrayList(AHConstants.ROLE_KJ_ADMINISTRATION_PRINCIPAL,
                        AHConstants.ROLE_KJ_ASSET_ADMIN, AHConstants.ROLE_KJ_DEPT_ASSET_ADMIN,
                        AHConstants.ROLE_KJ_DEPT_PRINCIPAL, AHConstants.ROLE_KJ_SYSTEM_USER);
                searchable.put("roleCodeArray", slRoles);
            } else if (AHConstants.BELONG_SL_CODE.equals(belongs)) {
                final List<String> slRoles = Lists.newArrayList(AHConstants.ROLE_SL_ASSET_BUYER,
                        AHConstants.ROLE_SL_ASSET_OWNER_ADMIN, AHConstants.ROLE_SL_ASSET_OWNER_PRINCIPAL,
                        AHConstants.ROLE_SL_DEPT_ASSET_ADMIN, AHConstants.ROLE_SL_DEPT_ASSET_PRINCIPAL);
                searchable.put("roleCodeArray", slRoles);
            }
        }
        // 科技中心部门可以看到下面所有二级部门的用户
        if ("kj".equalsIgnoreCase(searchable.getStrValue("orgCode"))) {
            final AuthOganization kjRoot = this.authOganizationRepository.findByCode("kj");
            final List<AuthOganization> subNodes = this.authOganizationRepository.findByParent(kjRoot);
            subNodes.add(kjRoot);
            final List<String> orgCodes = subNodes.stream().map(org -> org.getCode()).collect(Collectors.toList());
            searchable.put("orgCodes", orgCodes);
        }
        if ("root".equalsIgnoreCase(searchable.getStrValue("orgCode"))) {
            final AuthOganization root = this.authOganizationRepository.findByCode("root");
            final List<AuthOganization> subNodes = this.authOganizationRepository.findByParent(root);
            subNodes.add(root);
            final AuthOganization kjRoot = this.authOganizationRepository.findByCode("kj");
            final List<AuthOganization> kjSubNodes = this.authOganizationRepository.findByParent(kjRoot);
            subNodes.addAll(kjSubNodes);
            final List<String> orgCodes = subNodes.stream().map(org -> org.getCode()).collect(Collectors.toList());
            searchable.put("orgCodes", orgCodes);
        }
        return PageResultDTO.success(this.authUserService.search(pageable, searchable));
    }

    @ApiOperation("查询组织用户")
    @GetMapping("/ex/org/{orgCode}")
    public ResultListDTO<AuthUserDTO> search(@PathVariable final String orgCode) {
        final AuthOganization org = this.authOganizationRepository.findByCode(orgCode);
        final List<AuthUser> users = org.getUsers();
        final List<AuthUserDTO> userArr =
                users.stream().map(it -> this.authUserConverter.toDTO(it)).collect(Collectors.toList());
        //        final List<AuthUserDTO> userArr = this.authUserConverter.toListDTO(users);

        return ResultListDTO.success(userArr);
    }

    @ApiOperation("分页查询所有用户")
    @GetMapping("/sl/p")
    public PageResultDTO<AuthUserDTO> slSearch(final Pageable pageable, final Searchable searchable) {
        final List<String> slRoles = Lists.newArrayList(AHConstants.ROLE_SL_ASSET_BUYER,
                AHConstants.ROLE_SL_ASSET_OWNER_ADMIN, AHConstants.ROLE_SL_ASSET_OWNER_PRINCIPAL,
                AHConstants.ROLE_SL_DEPT_ASSET_ADMIN, AHConstants.ROLE_SL_DEPT_ASSET_PRINCIPAL);
        searchable.put("roleCodeArray", slRoles);
        return PageResultDTO.success(this.authUserService.search(pageable, searchable));
    }

    @ApiOperation("分页查询所有用户")
    @GetMapping("/kj/p")
    public PageResultDTO<AuthUserDTO> kjSearch(final Pageable pageable, final Searchable searchable) {
        final List<String> slRoles = Lists.newArrayList(AHConstants.ROLE_KJ_ADMINISTRATION_PRINCIPAL,
                AHConstants.ROLE_KJ_ASSET_ADMIN, AHConstants.ROLE_KJ_DEPT_ASSET_ADMIN,
                AHConstants.ROLE_KJ_DEPT_PRINCIPAL, AHConstants.ROLE_KJ_SYSTEM_USER);
        searchable.put("roleCodeArray", slRoles);
        return PageResultDTO.success(this.authUserService.search(pageable, searchable));
    }

    @ApiOperation("查询所有科技用户")
    @GetMapping("/kj/all")
    public ResultListDTO<AuthUserDTO> index(final Searchable searchable) {
        final AuthOganization kjRoot = this.authOganizationRepository.findByCode("kj");
        final List<AuthOganization> subNodes = this.authOganizationRepository.findByParent(kjRoot);
        subNodes.add(kjRoot);
        final List<String> orgCodes = subNodes.stream().map(org -> org.getCode()).collect(Collectors.toList());
        searchable.put("orgCodes", orgCodes);
        final List<AuthUserDTO> DTOs = this.authUserService.findAll(searchable);
        final ResultListDTO<AuthUserDTO> result = ResultListDTO.success(DTOs);
        return result;
    }
}
